// pages/match/match.js
const app =getApp()
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    modalName: '',
    modalName2: '',
    modalName3: '',
    modalName5: '',
    img: '',
    RoomName: '',
    RoomID: 0,
    UserID: '',
    notice: '',
    haveImg: app.globalData.haveImg,
    bgimg1: app.globalData.bgimg
  },
  attached() {
    this.setData({
      bgimg1: app.globalData.bgimg,
      haveImg: app.globalData.haveImg
    })


  },
  methods: {
  establish:function(){
    console.log("OK")
    // wx.navigateTo({
    //   url: '/pages/others/establishChatroom/establishChatroom',
    // })
    this.setData({
      modalName: 'DialogModal'
    })
  },
  hideModal:function()
  {
    this.setData({
      modalName2: '',
      modalName3: '',
      notice: '',
      modalName:'',
      modalName5: '',
    })
  },
  searchRoom:function()
  {
    // console.log("OK")
    this.setData({
      modalName2: 'DialogModal2'
    })
  },
  searchRoom2: function (e) {
    this.data.RoomID = e.detail.value
  },

  searchUser: function () {
    // console.log("OK")
    this.setData({
      modalName3: 'DialogModal3'
    })

  },
  searchUser2: function (e) {
    this.data.UserID = e.detail.value
  },
 
  update:function()
  {
    //创建聊天室
    this.setData({
      modalName: ''
    })
    var that=this
    if(this.data.RoomName=='') console.log("NULL")
    console.log(app.globalData.userInfo.token)
    console.log(this.data.RoomName)
    var token = app.globalData.userInfo.token
    var name = this.data.RoomName
    var img=this.data.img
    console.log(img)

    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/newChatRoom',
      method:'POST',
      data:{
        token: token,
        roomName: name,
        imgURL:img
      },
      success:function(e)
      {
        console.log(e)
        //提醒 成果与否
        if(e.data.chatRoom==null)
        {
        that.setData({
          notice:'notice',
          errmsg: e.data.errmsg
        })
        }else{
          that.setData({
            notice: 'notice',
            errmsg: "恭喜你创建成功"
          })
        }
      }
    })
    //刷新聊天室列表
    wx.request({
      // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
      method: 'POST',
      data: {
        token: app.globalData.userInfo.token,
        userId: app.globalData.userInfo.userInfo.id
      },
      success: function (res) {
        console.log(res)
        app.globalData.RoomCount = res.data.list
      }
    })
  },
  update2:function()
  {
    this.setData({
      modalName2: ''
    })
    var that=this;
    //查找房间
    var token = app.globalData.userInfo.token
    // console.log(token)
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/searchChatRoom',
      method: 'POST',
      data:
      {
        token: token,
        key: that.data.RoomID
      },
      success: function (res) {
        console.log(res)
        //如果存在room
        if (res.data.list[0] != null) {
          that.setData({
            modalName5:'room',
            list:res.data.list[0]
          })
        }
        else{
          that.setData({
            notice: 'notice',
            errmsg: "不好意思，对应聊天室ID的房间不存在"
          })
        }
      }
    })

    
  },
  update3:function()
  {
    //查找用户
    this.setData({
      modalName3: ''
    })
    if (this.data.UserID == '') console.log("NULL")
    console.log(app.globalData.userInfo.token)
    // console.log(this.data.UserID)
    var that=this
    wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/searchUser',
      method: 'POST',
      data:
      {
        token: app.globalData.userInfo.token,
        key: that.data.UserID
      },
      success: function (res) {
        console.log(res)
        //如果存在room
        if (res.data.list[0] != null) {
          that.setData({
            modalName5: 'friend',
            list: res.data.list[0]
          })
        }
        else {
          that.setData({
            notice: 'notice',
            errmsg: "不好意思，对应用户ID的房间不存在"
          })
        }
      }
    })


  },
  
  addPerson:function(e)
  {
    this.setData({
      modalName5: '',
    })
    var that =this
    console.log(e.currentTarget.dataset)
    //先邀请而不是加入
    if (e.currentTarget.dataset.type=='room')
    {
      wx.request({
      url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/chatInfo/joinChatRoom',
      method: 'POST',
      data:
      {
        token: app.globalData.userInfo.token,
        roomId: e.currentTarget.dataset.id* 1
      },
      success: function (res) {
        console.log(res)
          if(res.data.bool==false)
          {
            that.setData({
              notice: 'notice',
              errmsg: res.data.errmsg
            })
          }
      }
    })
    //刷新房间列表
      wx.request({
        // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/chat/getChatRoomList',
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getChatRoomListByUserId',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          userId: app.globalData.userInfo.userInfo.id
        },
        success: function (res) {
          console.log(res)
          app.globalData.RoomCount = res.data.list
        }
      })
    }
    else{
      //好友
      // console.log(e.currentTarget.dataset.id * 1)
      wx.request({
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/addFriend',
        method: 'POST',
        data:
        {
          token: app.globalData.userInfo.token,
          userId: e.currentTarget.dataset.id
        },
        success: function (res) {
          console.log(res)
          if(res.data.bool==false)
          {
            that.setData({
              notice:'notice',
              errmsg: res.data.errmsg
            })
          }
        }
      })
      //刷新用户列表
      wx.request({
        // url: 'https://mockapi.eolinker.com/SN5C9Ybd511179abd037ba7a2443cbd1f4885103f842b3e/user/getFriendsList',
        url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/user/getFriendsList',
        method: 'POST',
        data: {
          token: app.globalData.userInfo.token,
          userId: app.globalData.userInfo.userInfo.id
        },
        success: function (res) {
          console.log(res)
          //除非有改动否则不用重新刷新
          app.globalData.FriendCount = res.data.list
        }
      })
    }
    
  },
  updateImg:function(){
    //创建群聊上传图片
    var that=this
    wx.chooseImage({
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        that.data.img = tempFilePaths[0]
        that.setData({
          img: tempFilePaths[0]
        })
        wx.uploadFile({
          url: 'https://sdu-miniprogram.cn:8888/api/miniprogram/fileInfo/upload',
          filePath: tempFilePaths[0],
          header: {
            "Content-Type": "multipart/form-data"
          },
          name: 'myFile',
          success: function (res) {
            var data = JSON.parse(res.data)
            console.log(data)
            that.data.img=data.url
            img:data.url
          }
        })
      },
    })
  },
  updateName:function(e)
  {
    //输入名字
    // console.log("ok")
    console.log(e)
    this.data.RoomName=e.detail.value
    // console.log(this.data.RoomName)

  },
  

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  }
  

  
})